﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.StoresService;
using ARR.UI.ExcelImporterService;
using DDF.Entity;
using ARR.UI.SpacesService;

namespace ARR.UI.Models
{    
    public interface IPlanogramsMasterModel : IModelBase
    {
        ObservableCollection<Planogram> Planograms { get; set; }

        ObservableCollection<Store> Stores { get; set; }


        void Save();

        void LoadPlanograms();

        List<PlanogramLocation> PlanogramLocations { get; set; }
    }


    public class PlanogramsMasterModel : ModelBase, IPlanogramsMasterModel
    {
        public PlanogramsMasterModel()
        {
            Planograms = new ObservableCollection<Planogram>();
            Stores = new ObservableCollection<Store>();

            LoadStores();
            LoadLocations();
            LoadPlanograms();
        }

        private void LoadLocations()
        {
            SpacesServiceClient service = new SpacesServiceClient();

            try
            {
                PlanogramLocations = service.GetAllPlanogramLocations();
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public ObservableCollection<Planogram> Planograms { get; set; }

        public ObservableCollection<Store> Stores { get; set; }

        public List<PlanogramLocation> PlanogramLocations { get; set; }

        public void Save()
        {
            var toSave = Planograms.Where(x => x.GetEntityState() != EntityState.Original);

            if (toSave.Any())
            {
                foreach (var p in toSave)
                {
                    if(p.PlanogramType.HasValue)
                        p.TypeId = (int)p.PlanogramType;

                    p.RangeReviewId = WorkspaceData.Instance.RangeReview.Id;
                }


                SpacesServiceClient service = new SpacesServiceClient();

                try
                {
                    service.SavePlanograms(toSave.ToList());
                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }
            }
        }


        private void LoadStores()
        {
            StoreServiceClient service = new StoreServiceClient();

            try
            {
                Stores = new ObservableCollection<Store>(service.GetByCountry(WorkspaceData.Instance.Country.Id, true));
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void LoadPlanograms()
        {
            SpacesServiceClient service = new SpacesServiceClient();

            try
            {
                var planograms = service.GetAllPlanograms(WorkspaceData.Instance.RangeReview.Id, false);
                Planograms = new ObservableCollection<Planogram>(planograms);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }
    }


    public class PlanogramsMasterModelMock : MockModelBase, IPlanogramsMasterModel
    {
        public PlanogramsMasterModelMock()
        {
            Planograms = new ObservableCollection<Planogram>();
            Stores = new ObservableCollection<Store>();

            LoadPlanograms();
        }

        public List<PlanogramLocation> PlanogramLocations { get; set; }

        public ObservableCollection<Planogram> Planograms { get; set; }

        public ObservableCollection<Store> Stores { get; set; }


        public void Save()
        {

        }

        public void LoadPlanograms()
        {
            
        }
    }
}
